const path =                    require('path');
const webpack =                 require('webpack');
const HtmlWebpackPlugin =       require('html-webpack-plugin');
const MiniCssExtractPlugin =    require("mini-css-extract-plugin");

const distPath = path.join(__dirname, './dist');

const stats = {
    hash: false,
    builtAt: false,
    version: false,
    modules: false,
    children: false,
    entrypoints: false,
    colors: {
        green: '\u001b[32m',
        yellow: '\u001b[32m',
    }
};

module.exports = {
    mode: 'development',
    entry: './app/index.js',
    devtool: 'source-map',
    context: path.resolve(__dirname, './src'),
    output: {
        path: distPath,
        filename: '[name]-[hash].js',
        publicPath: '/',
    },
    optimization: {
        splitChunks: {
            cacheGroups: {
                commons: {
                    test: /[\\/]node_modules[\\/]/,
                    name: "vendor",
                    chunks: "all"
                }
            }
        }
    },
    module: {
        rules: [
            {
                test: /\.(js|jsx)$/,
                exclude: /node_modules/,
                use: [
                    'babel-loader',
                ]
            },
            {
                test: /\.css$/,
                use: [
                    MiniCssExtractPlugin.loader,
                    // 'style-loader',
                    {loader: "css-loader"},
                ]
            },
            {
                test: /\.scss$/,
                exclude: /node_modules/,
                use: [
                    MiniCssExtractPlugin.loader,
                    // 'style-loader',
                    {loader: "css-loader"},
                    'sass-loader',
                ],
            },
            {
                test: /\.(eot?.+|svg?.+|ttf?.+|otf?.+|woff?.+|woff2?.+)$/,
                use: 'file-loader?name=assets/[name].[ext]'
            },
            {
                test: /\.(jpg|jpeg|png|gif|ico|svg)$/,
                use: [
                    // if less than 10Kb, bundle the asset inline, if greater
                    // folder using file-loader
                    'url-loader?limit=10240&name=assets/[name].[ext]'
                ]
            }
        ],
    },
    resolve: {
        extensions: ['.webpack-loader.js', '.web-loader.js', '.loader.js', '.js', '.jsx'],
        modules: [
            path.resolve(__dirname, 'node_modules'),
            './',
        ]
    },
    plugins: [
        new webpack.ProvidePlugin({
            _: 'lodash',
            QRious: 'qrious',
            JsBarcode: 'jsbarcode',
        }),
        new HtmlWebpackPlugin({
            path: distPath,
            template: './public/index.dev.html',
            filename: 'index.html',
            chunks: ['vendor', 'main'],
        }),
        new MiniCssExtractPlugin({filename: "[name]-[hash].css"}),
    ],
    devServer: {
        port: 3000,
        host: '127.0.0.1',
        stats,
        inline: true,
        compress: false,
        disableHostCheck: true,
        historyApiFallback: true,
    }
};
